package cn.star.jots.user.controller;

import cn.star.jots.base.response.JsonResult;
import cn.star.jots.user.pojo.dto.UserQuery;
import cn.star.jots.user.pojo.dto.UserSaveParam;
import cn.star.jots.user.pojo.dto.UserSavePassword;
import cn.star.jots.user.pojo.dto.UserUpdateParam;
import cn.star.jots.user.pojo.vo.UserVO;
import cn.star.jots.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/v1/user")
@Slf4j
@Api(tags = "用户管理")
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("/select")
    @ApiOperation(value = "查询用户列表")
    public JsonResult select(UserQuery userQuery) {
        log.info("查询用户列表:userQuery={}", userQuery);
        List<UserVO> list = userService.selectAll(userQuery);
        return JsonResult.ok(list);
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存用户信息")
    public JsonResult saveInfo(UserSaveParam userSaveParam) {
        log.info("保存用户信息:userSaveParam={}", userSaveParam);
        userService.saveInfo(userSaveParam);
        return JsonResult.ok();
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除用户")
    public JsonResult delete(@PathVariable String id) {
        log.info("删除用户:id={}", id);
        userService.delete(id);
        return JsonResult.ok();
    }

    @DeleteMapping("/delete/batch/{ids}")
    @ApiOperation(value = "批量删除用户")
    public JsonResult deleteBatch(@PathVariable List<String> ids) {
        log.info("批量删除用户:ids={}", ids);
        userService.deleteBatch(ids);
        return JsonResult.ok();
    }

    @PutMapping("/update")
    @ApiOperation(value = "更新用户信息")
    public JsonResult updateInfo(UserUpdateParam userUpdateParam){
        log.info("更新用户信息:userUpdateParam={}", userUpdateParam);
        userService.updateInfo(userUpdateParam);
        return JsonResult.ok();
    }

    @PutMapping("/savePassword")
    @ApiOperation(value = "修改密码")
    public JsonResult updateSavePassword(UserSavePassword userSavePassword){
        log.info("修改密码:userSavePassword={}", userSavePassword);
        userService.savePassword(userSavePassword);
        return JsonResult.ok();
    }
}
